import pandas as pd
import matplotlib.pyplot as plt
import numpy as np

# Read the noisy signal file before filtering
data = pd.read_csv("2signal_data.csv", names=["Time", "Signal"])

# Read the filtered signal data
filtered_data = pd.read_csv("5Filtered_signal.csv")

# Calculate the original sine wave, assuming known frequency and amplitude
frequency = 5.0  # Sine wave frequency, same as the generated signal
amplitude = 1  # Sine wave amplitude, same as the generated signal
clean_signal = amplitude * np.sin(2 * np.pi * frequency * filtered_data["Time"])

# Plot the time-domain graph along with the noisy signal before filtering
plt.figure(figsize=(12, 6))
plt.plot(filtered_data["Time"], filtered_data["FilteredSignal"], label="Filtered Signal")
plt.plot(data["Time"], data["Signal"], label="Noisy Signal", linestyle='--')
# plt.plot(filtered_data["Time"], clean_signal, label="Original Sin Wave", linestyle='--')
plt.title("Time Domain Signal (Filtered)")
plt.xlabel("Time (s)")
plt.ylabel("Amplitude")
plt.legend()
plt.grid()
plt.show()

# Before filtering
# Implement DFT manually
def compute_dft1(signal):
    N = len(signal)
    dft1 = []
    for k in range(N):
        real_part1 = sum(signal[n] * np.cos(2 * np.pi * k * n / N) for n in range(N))
        imag_part1 = -sum(signal[n] * np.sin(2 * np.pi * k * n / N) for n in range(N))
        dft1.append(np.sqrt(real_part1**2 + imag_part1**2))  # Magnitude of complex number
    return np.array(dft1[:N // 2])  # Return only the positive frequencies

Sampling_rate1 = 100.0  # Sampling rate

# Retrieve the signal
signal = data["Signal"].values

# Compute DFT
dft_result = compute_dft1(signal)

# Frequency and magnitude
N1 = len(signal)
Sampling_rate1 = 100.0  # Sampling rate
frequencies = np.linspace(0, Sampling_rate1 / 2, N1 // 2)

# fft_magnitude = np.abs(dft_result[:N1 // 2]) / N1
fft_magnitude = compute_dft1(data["Signal"])

# Compute the spectrum of the filtered signal
# fft_filtered = compute_dft2(filtered_data["FilteredSignal"])

'''
# Frequency-domain graph
plt.figure(figsize=(12, 6))
plt.plot(frequencies, fft_magnitude, label="Frequency Spectrum")
plt.title("Frequency Domain Signal")
plt.xlabel("Frequency (Hz)")
plt.ylabel("Amplitude")
plt.legend()
plt.grid()
plt.show()
'''

# After filtering
# Implement DFT manually
def compute_dft2(signal):
    N = len(signal)
    dft2 = []
    for k in range(N):
        real_part2 = sum(signal[n] * np.cos(2 * np.pi * k * n / N) for n in range(N))
        imag_part2 = -sum(signal[n] * np.sin(2 * np.pi * k * n / N) for n in range(N))
        dft2.append(np.sqrt(real_part2**2 + imag_part2**2))  # Magnitude of complex number
    return np.array(dft2[:N // 2])  # Return only the positive frequencies

# Set the sampling rate
sampling_rate = 100.0  # Based on the settings in the signal generation code
N2 = len(filtered_data["FilteredSignal"])  # Number of signal points

# Compute the frequency axis
frequencies = np.linspace(0, sampling_rate / 2, N2 // 2)

# Compute the spectrum of the filtered signal
fft_filtered = compute_dft2(filtered_data["FilteredSignal"])

# Plot the frequency-domain graph
plt.figure(figsize=(12, 6))
plt.plot(frequencies, fft_filtered, label="Filtered Signal Spectrum")
plt.plot(frequencies, fft_magnitude, label="Noisy Signal Spectrum", linestyle='--')  # Before filtering
plt.title("Frequency Domain Signal (Filtered)")
plt.xlabel("Frequency (Hz)")
plt.ylabel("Amplitude")
plt.legend()
plt.grid()
plt.show()
